//location.js
//获取应用实例
var width;
var height;
wx.getSystemInfo({
  success: function (res) {
    width = res.windowWidth;
    height = res.windowHeight;
  }
});
var controlRight = parseInt(width) * 0.1;
var app = getApp();
Page({
  //页面加载事件
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var phone = options.phone;
    var appData = app.appData;
    var systemUser = appData.systemUser;
    var ststus = systemUser.status;
    var that = this
    //同步获取到所查看用户轨迹行程
    that.setTimeoutEvent = setInterval(function () {
      wx.request({
        //请求后台历史记录
        url: appData.contextPath + 'queryLatlon',
        data: {
          phone: phone
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          var latlonArray = res.data;
          
          var polyline = that.data.polyline
          for(var i = 0;i<latlonArray.length && i<2; i++){
            polyline[i].points = latlonArray[i].locations
          }    
          that.setData({
            polyline: polyline,
            latitude: latlonArray[0].locations[0].latitude,
            longitude: latlonArray[0].locations[0].longitude
          });
        },
      });
      }, 1000);
  },
  data: {
    phone: '',
    scale: 15,
    longitude: '',
    latitude: '',
    width: width,
    height: height,
    circles: [{

      color: '#FF0000DD',
      fillColor: '#7cb5ec88',
      radius: 1000,
      strokeWidth: 1
    }],
    markers: [
    ],
    polyline: [
      {
        points: [],
        color: "#00ff00",       
        width: 6,
        arrowLine: true
      },{
        points: [],
        color: "#ff0000",
        width: 6,
        arrowLine: true
      }],
      
    controls: [
      {
        id: 1,
        iconPath: '../../image/search.png',
        position: {
          width: width,
          height: 50,
          top: 20,
          right: controlRight
        },
        clickable: true
      },
      {
        id: 2,
        iconPath: '../../image/jia.png',
        position: {
          width: 30,
          height: 30,
          top: 200 - 50,
          left: 280
        },
        clickable: true
      },
      {
        id: 3,
        iconPath: '../../image/jian.png',
        position: {
          width: 30,
          height: 30,
          top: 230 - 50,
          left: 280
        },
        clickable: true
      },
      {
        id: 4,
        iconPath: '../../image/location-control.png',
        position: {
          width: 30,
          height: 30,
          top: 280,
          left: 280
        },
        clickable: true
      },
      {
        id: 5,
        iconPath: '../../image/route.png',
        position: {
          width: 40,
          height: 40,
          top: 350,
          left: 275
        },
        clickable: true
      }, 
    ]
  },
  onShow: function () {

  },

  onReady: function () {
    var status = app.appData.systemUser.status;
    if (status == '未登录') {
      wx.redirectTo({
        url: '../login/login',
        success: function () {
          wx.hideTabBar({
            aniamtion: true,
            success: function () {
            }
          });
        },
        fail: function (e) {
          console.log('eeeeeeeeeeeeeeee' + e)
        }
      })
    }
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        that.setData({
          // latitude: res.latitude,
          // longitude: res.longitude,
          markers: [{
            iconPath: '../../image/0.gif',
            title: '南昌站',

            id: 0,
            latitude: 28.662991163527906,
            longitude: 115.91763209358216,
            width: 25,
            height: 25,

          },
          {
            iconPath: '../../image/1.gif',
            title: '检测大厦',

            id: 1,
            latitude: 28.66371,
            longitude: 115.92804,
            width: 25,
            height: 25
          },
          {
            iconPath: '../../image/2.gif',
            title: '青山湖景区',

            id: 2,
            latitude: 28.693688345437906,
            longitude: 115.92312525764466,
            width: 35,
            height: 25
          },
          {
            iconPath: '../../image/6.gif',
            title: '英雄大桥',

            id: 3,
            latitude: 28.714432490959112,
            longitude: 115.9147747094345,
            width: 25,
            height: 30
          },
          {
            iconPath: '../../image/8.gif',
            title: '孔目湖',

            id: 4,
            latitude: 28.738840131557982,
            longitude: 115.88743227073668,
            width: 30,
            height: 25
          },
          {
            iconPath: "../../image/jxlg1.gif",
            title: "江西理工大学",

            id: 5,
            latitude: 28.74154104464624,
            longitude: 115.876900067482,
            width: 25,
            height: 25
          }
          ],
          //圆的控件
          circles: [{
            latitude: res.latitude,
            longitude: res.longitude,
            color: '#FF0000DD',
            fillColor: '#7cb5ec88',
            radius: 1000,
            strokeWidth: 1
          }]
        })
      }
    });

  },
  controltap(e) {
    var that = this;
    //地图放大控件
    if (e.controlId == 2) {
      that.setData({
        scale: ++that.data.scale
      });
    }
    //地图缩小控件
    else if (e.controlId == 3) {
      that.setData({
        scale: --that.data.scale
      });
    }
    //进入搜索地点
    else if (e.controlId == 1) {
      wx.chooseLocation({
        success: function (res) {
          console.log(res);
          wx.openLocation({
            latitude: res.latitude,
            longitude: res.longitude,
            name: res.name
          })
          //刷新页面数据


        },
        fail: function () {
          console.log('选择位置出错!');
        }
      })
    }
    //点击中心空间，获得当前的位置
    else if (e.controlId == 4) {
      var that = this;
      wx.getLocation({
        type: 'gcj02',
        success: function (res) {
          that.setData({
            latitude: res.latitude,
            longitude: res.longitude,
          })
        },
      })
    }
    //点击路线空间，跳转到搜索页面
    else if (e.controlId == 5) {
      wx.navigateTo({
        url: '../../pages/detail/detail',
        success: function () {
          console.log('跳转成功')
        },
        fail: function () {
          console.log('跳转失败');
        }
      })
    }
    //行走记录
    else if (e.controlId == 6) {
      if (this.data.controls[5].iconPath == '../../image/circle1.png') {
        var points = this.data.polyline[0].points;
        if (points.length > 0) {
          try {
            var latlon = new Object();
            var userId = app.appData.systemUser.id;
            var phone = app.appData.systemUser.phone;
            wx.request({
              url: app.appData.contextPath + 'latlonServlet',
              data: {
                latlon: JSON.stringify(points),
                userId: userId,
                phone: phone
              },
              header: {
                'content-type': 'application/json' // 默认值
              }
            })
            console.log(latlon);
          } catch (e) {
            console.error("访问服务出错!,可能服务器IP地址的更改或服务器没有启动", e);
          }

        }

        this.data.controls[5].iconPath = '../../image/circle2.png';
        clearInterval(this.setTimeoutEvent);
      } else {
        this.data.controls[5].iconPath = '../../image/circle1.png';

        that.setTimeoutEvent = setInterval(function () {

          wx.getLocation({
            type: 'gcj02', //返回可以用于wx.openLocation的经纬度
            success: function (res) {
              var polyline = that.data.polyline;
              polyline[0].points.push({

                latitude: res.latitude,
                longitude: res.longitude

              });
              console.log(polyline[0].points);
              that.setData({ polyline: polyline });
            }
          });

        }, 5000);

      }
      this.setData({
        controls: this.data.controls
      });


    }
  },
  cunru: function () {

  },
  markertap: function (e) {
    // 可以改变标记点的图片，也可以显示 一段 描述
    wx.showToast({
      title: "3"
    });
    console.log(e);
  },
  markertap(e) {
    var that = this;
    if (e.markerId == 0) {
      wx.navigateTo({
        url: '../../pages/ncz/ncz',
        success: function () {
          console.log('跳转成功')
        },
        fail: function () {
          console.log('跳转失败');
        }
      })
    }
    else if (e.markerId == 1) {
      wx.navigateTo({
        url: '../../pages/jc/jc',
        success: function () {
          console.log('跳转成功')
        },
        fail: function () {
          console.log('跳转失败');
        }
      })
    }
    else if (e.markerId == 2) {
      wx.navigateTo({
        url: '../../pages/qsh/qsh',
        success: function () {
          console.log('跳转成功')
        },
        fail: function () {
          console.log('跳转失败');
        }
      })
    }
    else if (e.markerId == 3) {
      wx.navigateTo({
        url: '../../pages/yxdq/yxdq',
        success: function () {
          console.log('跳转成功')
        },
        fail: function () {
          console.log('跳转失败');
        }
      })
    }
    else if (e.markerId == 4) {
      wx.navigateTo({
        url: '../../pages/kmh/kmh',
        success: function () {
          console.log('跳转成功')
        },
        fail: function () {
          console.log('跳转失败');
        }
      })
    }
    else if (e.markerId == 5) {
      wx.navigateTo({
        url: '../../pages/jxlg/jxlg',
        success: function () {
          console.log("跳转成功")
        },
        fail: function () {
          console.log("跳转失败")
        }
      })
    }
  },
  back: function () {
    clearInterval(this.setTimeoutEvent);
    wx.switchTab({
      url: '../index/index'
    });
  },
})
